package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.vo.TInnovationAchievementDetailVo;
import org.tuoren.official.domain.bo.TInnovationAchievementDetailBo;
import org.tuoren.official.service.ITInnovationAchievementDetailService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 研究成果列
 *
 * @author Lion Li
 * @date 2024-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/achievementDetail")
public class TInnovationAchievementDetailController extends BaseController {

    private final ITInnovationAchievementDetailService tInnovationAchievementDetailService;

    /**
     * 查询研究成果列列表
     */
    @SaCheckPermission("official:innovationAchievementDetail:list")
    @GetMapping("/list")
    public TableDataInfo<TInnovationAchievementDetailVo> list(TInnovationAchievementDetailBo bo, PageQuery pageQuery) {
        return tInnovationAchievementDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出研究成果列列表
     */
    @SaCheckPermission("official:innovationAchievementDetail:export")
    @Log(title = "研究成果列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TInnovationAchievementDetailBo bo, HttpServletResponse response) {
        List<TInnovationAchievementDetailVo> list = tInnovationAchievementDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "研究成果列", TInnovationAchievementDetailVo.class, response);
    }

    /**
     * 获取研究成果列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:innovationAchievementDetail:query")
    @GetMapping("/{id}")
    public R<TInnovationAchievementDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tInnovationAchievementDetailService.queryById(id));
    }

    /**
     * 新增研究成果列
     */
    @SaCheckPermission("official:innovationAchievementDetail:add")
    @Log(title = "研究成果列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TInnovationAchievementDetailBo bo) {
        return toAjax(tInnovationAchievementDetailService.insertByBo(bo));
    }

    /**
     * 修改研究成果列
     */
    @SaCheckPermission("official:innovationAchievementDetail:edit")
    @Log(title = "研究成果列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TInnovationAchievementDetailBo bo) {
        return toAjax(tInnovationAchievementDetailService.updateByBo(bo));
    }

    /**
     * 删除研究成果列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:innovationAchievementDetail:remove")
    @Log(title = "研究成果", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tInnovationAchievementDetailService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 官网产学研用研究成果列表
     */
    @SaIgnore
    @GetMapping("/getlist")
    public TableDataInfo<TInnovationAchievementDetailVo> getlist() {
        return tInnovationAchievementDetailService.getlist();
    }

    /**
     * 官网产学研用研究成果详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/detail/{id}")
    public R<TInnovationAchievementDetailVo> getDetailById(@NotNull(message = "id不能为空")
                                                     @PathVariable Long id) {
        return R.ok(tInnovationAchievementDetailService.queryById(id));
    }
}
